package cn.lcsjsxy.yxc.multithreading.synchronization;

class Window1 implements Runnable {
    private int TICKET = 100;

    @Override
    public void run() {
        while (true) {
            show();
            if (TICKET<=0)
                break;
        }
    }

    public synchronized void show() {
        if (TICKET > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "售票中，票号为：" + TICKET);
            TICKET--;
        }
    }

}

public class WindowSynchronizationRunnableTest {  //同步监视器：this
    public static void main(String[] args) {
        Window1 window = new Window1();
        Thread window1 = new Thread(window);
        Thread window2 = new Thread(window);
        Thread window3 = new Thread(window);
        window1.setName("WindowOne");
        window2.setName("WindowTwo");
        window3.setName("WindowThree");
        window1.start();
        window2.start();
        window3.start();
    }
}
